﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ProxyWrapper;
using System.Text;
using System.Data;
public partial class CreateUser : SecPage
{
    private ProxyWrapper.UserProxy.UserDetails _userDetail = null;
    private ProxyWrapper.UserProxy.UserDetails _objUserObject = null;
    private string _userId = "0";
    public string UserId { get { return _userId; } }
    private string _userName = "";
    public string UserName { get { return _userName; } }
    private string _password = "";
    public string Password { get { return _password; } }
    private string _deviceCount;
    public string DeviceCount { get { return _deviceCount; } }
    private bool _deviceCheck = false;
    public string DeviceCountCheck
    {
        get {
            if (_deviceCheck)
                return "checked";
            else 
                return "";
        }
    }
    private string _buttonText = "Create";
    public string ButtonName { get { return _buttonText; } }
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            _objUserObject = ((ProxyWrapper.UserProxy.UserDetails)Session["UserObject"]);
            if (Request.QueryString != null && Request.QueryString["msg"] != null)
            {
                showMessages.Visible = true;
                spMsg.InnerText = Request.QueryString["msg"].ToString();
            }
            if (Request.QueryString != null && Request.QueryString["userid"] != null)
            {
                _userId = Request.QueryString["userid"].ToString();
                DrawPage(_userId);
            }
            // LoadServiceList();
        }
        catch (Exception ex)
        {
        }
    }

    protected string LoadServiceList()
    {
        try
        {
            int i = 0;
            ProxyWrapper.ServicesServiceWrapper _services = new ServicesServiceWrapper();
            Dictionary<int, string> serviceData = new Dictionary<int, string>();
            Dictionary<int, string> userServiceData = new Dictionary<int, string>();
            DataSet dsServices = _services.GetServiceList(_objUserObject.ClientId, _objUserObject.HashCode);
            if (dsServices != null && dsServices.Tables.Count > 0 && dsServices.Tables[0].Rows.Count > 0)
            {
                for (int k = 0; k < dsServices.Tables[0].Rows.Count; k++)
                {
                    serviceData.Add(Convert.ToInt32(dsServices.Tables[0].Rows[k][0]), Convert.ToString(dsServices.Tables[0].Rows[k][1]));
                }

            }

            if (_userDetail != null && _userDetail.GetServiceList != null && _userDetail.GetServiceList.Tables.Count > 0 && _userDetail.GetServiceList.Tables[0].Rows.Count > 0)
            {
                for (int j = 0; j < _userDetail.GetServiceList.Tables[0].Rows.Count; j++)
                {
                    userServiceData.Add(Convert.ToInt32(_userDetail.GetServiceList.Tables[0].Rows[j][0]), Convert.ToString(_userDetail.GetServiceList.Tables[0].Rows[j][1]));
                }

            }
            string elemnts = "<li><input type='checkbox' id='inputee_{0}' class='inputcheckbox' name='selectedServices[]' value='{1}' {2} /><label class='selectbox_label' for='inputee_{3}'>{4}</label>"
                            + "     <a title='Edit {5}' target='_blank' href='CreateService.aspx?serviceid={6}'> <img style=margin: 0px 0px 10px 2px; src=Images/external_link.gif></a></li>";
            object[] args = null;
            StringBuilder sb = new StringBuilder();
            foreach (KeyValuePair<int, string> item in serviceData)
            {
                args = new object[7];
                args[0] = i;
                args[1] = item.Key;
                if (userServiceData != null && userServiceData.Count > 0)
                {
                    if (userServiceData.ContainsKey(item.Key))
                        args[2] = "checked";
                    else
                        args[2] = "";

                }
                else
                    args[2] = "";

                args[3] = i;
                args[4] = item.Value;
                args[5] = item.Value;
                args[6] = item.Key;
                i++;
                sb.Append(string.Format(elemnts, args));
            }
            return sb.ToString();
        }
        catch (Exception)
        {
            return "No Service Found";
        }
    }

    private void DrawPage(string userId)
    {

        ProxyWrapper.UserServiceWrapper _usr = new UserServiceWrapper();
        _userDetail = _usr.GetAppUser(Convert.ToInt32(userId), _objUserObject.UserId, _objUserObject.HashCode);
        _userId = _userDetail.UserId.ToString();
        _userName = _userDetail.UserName;
        _password = _userDetail.Password;
        _deviceCount = _userDetail.DeviceLimit.ToString();
        if (_userDetail.DeviceLimit > 0)
            _deviceCheck = true;
        else
            _deviceCheck = false;

        _buttonText = "Save Changes";


    }
}